<template>
  <el-dialog
    title="产品订单选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal="false"
    width="80%"
    center
  >
    <el-table
      v-loading="loading"
      :data="productList"
      show-summary
      @current-change="handleCurrent"
      @row-dblclick="handleRowDbClick"
    >
      <el-table-column width="60" align="center">
        <template v-slot="scope">
          <el-radio
            v-model="selectedProduct"
            :label="scope.row.itemId"
            @change="handleRowChange(scope.row)"
            >{{ " " }}
          </el-radio>
        </template>
      </el-table-column>
      <el-table-column label="产品编码" align="center" prop="itemCode" />
      <el-table-column label="产品名称" align="center" prop="itemName" />
      <el-table-column
        label="规格型号"
        align="center"
        prop="specification"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="总价" align="center" prop="totalAmount" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { orderListLine } from '@/api/mes/xs/line'

export default {
  name: "product",
  props: {
    sourceCode: null,
  },
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      selectedProduct: undefined,

      selectedRows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售订单表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        soNumber: this.sourceCode,
        itemId: null,
        itemCode: null,
        itemName: null,
      },
    };
  },
  watch: {
    sourceCode(val) {
      this.queryParams.soNumber = val;
      this.getList();
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销售订单列表 */
    getList() {
      this.loading = true;
      orderListLine(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    //双击选中
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit("onSelected", this.selectedRows);
        this.showFlag = false;
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedProduct == null || this.selectedProduct == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!",
        });
        return;
      }
      this.$emit("onSelected", this.selectedRows);
      this.showFlag = false;
    },
  },
};
</script>
